package com.qzh.G.utils;

import java.math.BigDecimal;
import java.security.SecureRandom;
import java.util.Random;
import java.util.UUID;

public class RandomUtil {
    /**
     * 输入触发概率，返回是否触发成功。
     * @return
     */
    public static boolean ask(BigDecimal bigDecimal){
             if(bigDecimal.compareTo(BigDecimal.ONE)>=0){
                    return true;
             }
             if(bigDecimal.compareTo(BigDecimal.ZERO)<=0){
                 return false;
             }
             //1-所传入的概率即为领域值，随机数需要大于等于领域值数才能触发成功
             BigDecimal fieldVar =BigDecimal.ONE.subtract(bigDecimal);
        Random random =new SecureRandom();
        return BigDecimal.valueOf(random.nextDouble()).compareTo(fieldVar) >= 0;
    }

    /**
     * 返回最小值最大值的其中一个值
     * 需要min和max均大于0
     * @param min
     * @param max
     * @return
     * @param <T>
     */
    public static double RandomBetween(double min, double max){
        //需要将min和max转换为（0，1）区间压缩
        double oldmin = min;double oldmax = max;
        min =min/oldmax;max = max/oldmax;
        SecureRandom random =new SecureRandom(UUID.randomUUID().toString().getBytes());
                        double result =random.nextDouble();
                 while(result<min||result>max) {
                     result = random.nextDouble();
                        }

                   result = result*oldmax;
        return result;
    }
}
